import React from "react";
import {inject, observer} from "mobx-react";
import SecondAuthCom from "../../../workflowpublic/components/second-auth/index";
import QYSAuthCom from "../../../workflowpublic/components/second-auth/QYSAuth";
import SecondAuthComStore from "../../../workflowpublic/stores/SecondAuthStore";
import Loading from "../AsyncLoading";

import * as authTypes from "../../constants/secondAuthType";
import base from "../../util/base";
import {verifyRemark} from "../../util/SignUtil";
import {closeQYSAuth} from "../../util/secondAuthUtil";

export default class Main extends React.Component {
    constructor(props) {
        super(props);
        this.state = {store: null};
    }

    componentDidMount() {
        this.setState({
            store: new SecondAuthComStore()
        });
    }

    componentWillMount() {
        this.controlBottomBtns(this.props);
    }

    componentWillReceiveProps(nextProps, nextContext) {
        if (this.props.visible != nextProps.visible) {
            this.controlBottomBtns(nextProps);
        }
    }

    controlBottomBtns(props) {
        const {allStores, wfOperateStore} = base;
        const {visible} = props;
        //验证的弹出框显示，禁用底部的按钮
        //验证的弹出框不显示，启用底部的浏览按钮
        wfOperateStore.controllBottomBtnDisable(visible);
    }

    //验证成功后的回调方法
    onAuthSuccess(result) {
        const {ctype = 0} = this.props;
        const {allStores} = base;
        const {secondAuthStore, wfSignInputStore} = allStores;
        secondAuthStore.controlDialogVisible(authTypes.SECOND_AUTH_DIALOG, false);  //关闭窗口

        //契约锁认证或者保护打开的窗口
        if (ctype == 1) {
            window.historyRoute.go(-1);
            this.controlBottomBtns({visible: false});
        }

        const {callbackFunc, callbackParam} = secondAuthStore;

        const {isProtectDialog, secondAuthType} = result;

        if (isProtectDialog == 1) {     //数据保护：CA或者契约锁数据保护
            secondAuthStore.setDialogResult({isProtectSuccess: 1});  //数据保护成功
        } else if (isProtectDialog == 10) {     //CA认证 + CA数据保护 或者 契约锁认证 + 契约锁数据保护
            secondAuthStore.setDialogResult({isProtectSuccess: 1});  //数据保护成功
            secondAuthStore.setDialogResult({isAuthSuccess: 1});  //二次验证成功
        } else {        //二次验证：CA或者契约锁验证
            secondAuthStore.setDialogResult({isAuthSuccess: 1});  //二次验证成功
        }

        //契约锁验证，保存印章信息
        if (secondAuthType == authTypes.QYS) {
            const {sealId: qysSealId, sealImg: qysSealImg, certDetails: qysCertDetails,sealSpec = {} } = result;    //契约锁印章，印章id信息
            let { width: qyswidth = 0, height: qysheight = 0 } = sealSpec;
            qyswidth *= 2.8346;
            qysheight *= 2.8346;
            secondAuthStore.setAuthResult({
                qysSealId,
                qysSealImg,
                qysCertDetails: JSON.stringify({ ...qysCertDetails, qyswidth, qysheight })
            });
        }

        wfSignInputStore.setForbidden(false);

        if (callbackFunc && typeof callbackFunc == "function") {
            callbackFunc.call({}, callbackParam);    //调用提交流程的方法
        }
    }


    //验证失败后的回调方法
    onAuthFail() {
        //nothing to do
    }

    //关闭窗口
    onClose() {
        const {ctype = 0} = this.props;
        const {allStores} = base;
        const {secondAuthStore} = allStores;
        secondAuthStore.controlDialogVisible(authTypes.SECOND_AUTH_DIALOG, false);

        //契约锁认证或者保护打开的窗口
        if (ctype == 1) {
            window.historyRoute.go(-1);
        }

        //关闭时契约锁认证的一些相关处理
        closeQYSAuth();
    }

    render() {
        const {store} = this.state;
        const {visible, ctype = 0} = this.props;
        const {allStores, wfOperateStore} = base;
        const {secondAuthStore} = allStores;
        const {protectType, isProtectDialog, authKey, sourcestr, signUrl} = secondAuthStore;
        let {secondAuthType} = secondAuthStore;

        if (isProtectDialog == 1) {
            secondAuthType = protectType;
        }

        if (!!store) {
            store.setAuthData({authKey, sourcestr, secondAuthType, isProtectDialog, signUrl});

            if (ctype == 1) {
                return <QYSAuthCom
                    secondAuthStore={store}
                    onSuccess={this.onAuthSuccess.bind(this)}
                    onFail={this.onAuthFail.bind(this)}
                    onClose={this.onClose.bind(this)}/>
            } else {
                return (
                    <SecondAuthCom
                        visible={visible}
                        secondAuthStore={store}
                        onSuccess={this.onAuthSuccess.bind(this)}
                        onFail={this.onAuthFail.bind(this)}
                        onClose={this.onClose.bind(this)}/>
                );
            }
        } else {
            return <Loading/>;
        }
    }
}
